﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;

namespace TS_XY_Train_Repaire.UserCon.Locomotive.Technology
{
    public partial class uc_Locomotive_MainBase_WorkFlow_TrainWorkPlace : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID, _LoadName, _LoadMWID, _LoadMWDID, _LoadMWSID = "";
        public uc_Locomotive_MainBase_WorkFlow_TrainWorkPlace()
        {
            InitializeComponent();
        }

        private void uc_Locomotive_MainBase_WorkFlow_TrainWorkPlace_Load(object sender, EventArgs e)
        {
            _LoadTreeList();
            _LoadWorkFlowList();
            tl_WorkFlow.SetFocusedNode(tl_WorkFlow.FindNodeByFieldValue("f_id", "G"+_LoadID));
            _LoadGridIn();
        }

        private void uc_Locomotive_MainBase_WorkFlow_TrainWorkPlace_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
        private void _LoadGridIn()
        {
            string strSQL = "select row_number() over (order by f_tdid,f_order) as f_row,f_id,f_name,f_brief from TS_TR_PS_Work_Main_Group_WorkPlace where f_del=0 and f_mwgid=" + _LoadID+ " order by f_tdid,f_order asc";
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, dt);
            gc_In.DataSource = dt;
        }
        private void _LoadGridOut()
        {
            string strSQL = "select row_number() over (order by f_tdid,f_order) as f_row,f_id,f_name,f_brief from TS_S_T_P_Train_WorkPlace where f_del=0  and f_tdid in (select f_id from TS_S_T_P_TrainDepSys where f_del=0 and (f_id="+ tl_Main.FocusedNode["f_id"].ToString().Substring(1) + " or f_level like '%,"+ tl_Main.FocusedNode["f_id"].ToString().Substring(1) + ",%')) and f_id not in (select f_wpid from TS_TR_PS_Work_Main_Group_WorkPlace where f_del=0 and f_mwgid=" + _LoadID + " ) order by f_tdid,f_order asc";
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, dt);
            gc_Out.DataSource = dt;
        }

        private void tl_Main_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (tl_Main.FocusedNode["f_id"].ToString().Substring(0, 1) == "D")
            {
                _LoadGridOut();
            }
            else
            {
                gc_Out.DataSource = null;
                gc_Out.Refresh();
            }
        }

        private void gc_Out_Click(object sender, EventArgs e)
        {
            if (gv_Out.SelectedRowsCount>0)
            {
                bt_Add.Enabled = true;
            }
            else
            {
                bt_Add.Enabled = false;
            }
        }

        private void gc_In_Click(object sender, EventArgs e)
        {
            if (gv_In.SelectedRowsCount > 0)
            {
                bt_Del.Enabled = true;
            }
            else
            {
                bt_Del.Enabled = false;
            }
        }

        private void tl_WorkFlow_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            bt_Add.Visible = false;
            bt_Del.Visible = false;
            try
            {
                gc_In.DataSource = null;
                gc_In.Refresh();
                gc_Out.DataSource = null;
                gc_Out.Refresh();
                if (tl_WorkFlow.FocusedNode["f_id"].ToString().Substring(0, 1) == "W")
                {
                }
                else
                {
                    switch (tl_WorkFlow.FocusedNode["f_id"].ToString().Substring(0, 1))
                    {
                        case "M":
                            break;
                        case "D":
                            break;
                        case "S":
                            break;
                        case "G":
                            bt_Add.Visible = true;
                            bt_Del.Visible = true;
                            _LoadID = tl_WorkFlow.FocusedNode["f_id"].ToString().Substring(1);
                            _LoadMWID = tl_WorkFlow.FocusedNode["f_mwid"].ToString();
                            _LoadMWDID = tl_WorkFlow.FocusedNode["f_mwdid"].ToString();
                            _LoadMWSID = tl_WorkFlow.FocusedNode["f_mwsid"].ToString();
                            _LoadGridIn();
                            _LoadGridOut();
                            break;
                    }
                }
            }
            catch
            {
                gc_In.DataSource = null;
                gc_In.Refresh();
                gc_Out.DataSource = null;
                gc_Out.Refresh();
            }
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_TR_PS_Work_Main_Group_WorkPlace set f_del=1,f_workerid="+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_id="+ gv_In.GetRowCellValue(gv_In.GetSelectedRows()[0], "f_id").ToString();
            TS_Win_Class.MySqlCom(strSQL);
            _LoadGridIn();
            _LoadGridOut();
            bt_Del.Enabled = false;
        }

        private void bt_Add_Click(object sender, EventArgs e)
        {
            string strSQL = "insert into TS_TR_PS_Work_Main_Group_WorkPlace(f_mwid,f_mwdid,f_mwsid,f_mwgid,f_wpid,f_name,f_brief,f_tcid,f_tdid,f_tdguid,f_typeid,f_perfid,f_order,f_must,f_photo,f_rank,f_dtid,f_drid,f_drname,f_dataunit,f_data1,f_data2,f_workerid) select "+_LoadMWID+ " as f_mwid," + _LoadMWDID + " as f_mwdid," + _LoadMWSID + " as f_mwsid," + _LoadID + " as f_mwgid,f_id as f_wpid,f_name,f_brief,f_tcid,f_tdid,f_tdguid,f_typeid,f_perfid,f_order,f_must,f_photo,f_rank,f_dtid,f_drid,f_drname,f_dataunit,f_data1,f_data2," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+" as f_workerid from TS_S_T_P_Train_WorkPlace where f_id="+ gv_Out.GetRowCellValue(gv_Out.GetSelectedRows()[0], "f_id").ToString();
            ;
            TS_Win_Class.MySqlCom(strSQL);
            _LoadGridIn();
            _LoadGridOut();
            bt_Add.Enabled = false;
        }

        private void _LoadTreeList()
        {
            LeverString_TrainDepSysList _tl = new LeverString_TrainDepSysList();
            tl_Main.Nodes.Clear();
            ArrayList pList = new ArrayList();
            string strSQL = "select * from TS_S_T_P_TrainClass where f_del=0";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                _tl = new LeverString_TrainDepSysList();
                _tl.f_id = "M" + read["f_id"].ToString();
                _tl.f_tcid = read["f_id"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_deep = "-1";
                _tl.f_level = ",";
                pList.Add(_tl);
            }
            read.Close();
            strSQL = "select *,(case f_type when 0 then '总成' when 1 then '转向架' when 2 then '电器' when 9 then '通用' else '未知' end ) as f_typename from TS_S_T_P_TrainDepSys where f_deep=0 and f_del=0";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            string _strUPID = "and (";
            int i = 0;
            while (read.Read())
            {
                _tl = new LeverString_TrainDepSysList();
                _tl.f_id = "D" + read["f_id"].ToString();
                _tl.f_tcid = read["f_tcid"].ToString();
                _tl.f_code = read["f_code"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_parent = "M" + read["f_tcid"].ToString();
                _tl.f_deep = read["f_deep"].ToString();
                _tl.f_type = read["f_type"].ToString();
                _tl.f_typename = read["f_typename"].ToString();
                _tl.f_level = read["f_level"].ToString();
                if (i == 0)
                {
                    _strUPID += "(f_level like '%," + read["f_id"].ToString() + ",%')";
                }
                else
                {
                    _strUPID += "or (f_level like '%," + read["f_id"].ToString() + ",%')";
                }
                pList.Add(_tl);
                i++;
            }
            _strUPID += ")";
            if (_strUPID.Length > 7)
            {
                read.Close();
                strSQL = "select *,(case f_type when 0 then '总成' when 1 then '转向架' when 2 then '电器' when 9 then '通用' else '未知' end ) as f_typename from TS_S_T_P_TrainDepSys where f_deep>0 and f_del=0 " + _strUPID + " order by f_deep,f_level,f_name asc";
                cmd = new SqlCommand(strSQL, conn);
                read = cmd.ExecuteReader();
                string _UPID = "";
                while (read.Read())
                {
                    _tl = new LeverString_TrainDepSysList();
                    _tl.f_id = "D" + read["f_id"].ToString();
                    _tl.f_name = read["f_name"].ToString();
                    _tl.f_deep = read["f_deep"].ToString();
                    _tl.f_level = read["f_level"].ToString();
                    _tl.f_type = read["f_type"].ToString();
                    _tl.f_typename = read["f_typename"].ToString();
                    _UPID = read["f_level"].ToString();
                    _UPID = _UPID.Substring(0, _UPID.Length - 1);
                    _UPID = _UPID.Substring(_UPID.LastIndexOf(',') + 1);
                    _tl.f_parent = "D" + _UPID;
                    pList.Add(_tl);
                }
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            tl_Main.DataSource = pList;
        }
        private void _LoadWorkFlowList()
        {
            TR_PS_Work_Main_List _tl = new TR_PS_Work_Main_List();
            tl_WorkFlow.Nodes.Clear();
            ArrayList pList = new ArrayList();
            _tl = new TR_PS_Work_Main_List();
            _tl.f_id = "W0";
            _tl.f_name = "整车作业流程目录";
            _tl.f_imgindex = 14;
            pList.Add(_tl);
            _tl = new TR_PS_Work_Main_List();
            _tl.f_id = "W1";
            _tl.f_name = "转向架作业流程目录";
            _tl.f_imgindex = 14;
            pList.Add(_tl);
            _tl = new TR_PS_Work_Main_List();
            _tl.f_id = "W2";
            _tl.f_name = "电器作业流程目录";
            _tl.f_imgindex = 14;
            pList.Add(_tl);
            string strSQL = "select * from TS_TR_PS_Work_Main where f_del=0";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                _tl = new TR_PS_Work_Main_List();
                _tl.f_id = "M" + read["f_id"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_parent = "W" + read["f_type"].ToString();
                _tl.f_rpid = read["f_rpid"].ToString();
                _tl.f_imgindex = 10;
                pList.Add(_tl);
            }
            read.Close();
            strSQL = "select * from TS_TR_PS_Work_Main_Day where f_mwid in (select f_id from TS_TR_PS_Work_Main where f_del=0) and f_del=0 order by f_mwid,f_order asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _tl = new TR_PS_Work_Main_List();
                _tl.f_id = "D" + read["f_id"].ToString();
                _tl.f_order = read["f_order"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_mwid = read["f_mwid"].ToString();
                _tl.f_mwdid = read["f_id"].ToString();
                _tl.f_length = read["f_length"].ToString() + "天";
                _tl.f_parent = "M" + read["f_mwid"].ToString();
                _tl.f_imgindex = 11;
                pList.Add(_tl);
            }
            read.Close();
            strSQL = "select * from TS_TR_PS_Work_Main_Steps where f_mwdid in (select f_id from TS_TR_PS_Work_Main_Day where f_mwid in (select f_id from TS_TR_PS_Work_Main where f_del=0) and f_del=0) and f_del=0 order by f_mwid,f_mwdid,f_order asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _tl = new TR_PS_Work_Main_List();
                _tl.f_id = "S" + read["f_id"].ToString();
                _tl.f_order = read["f_order"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_mwid = read["f_mwid"].ToString();
                _tl.f_mwdid = read["f_mwdid"].ToString();
                _tl.f_mwsid = read["f_id"].ToString();
                _tl.f_length = read["f_length"].ToString() + "小时"; ;
                _tl.f_stime = read["f_stime"].ToString();
                _tl.f_etime = read["f_etime"].ToString();
                _tl.f_parent = "D" + read["f_mwdid"].ToString();
                _tl.f_imgindex = 12;
                pList.Add(_tl);
            }
            read.Close();
            strSQL = "select * from TS_TR_PS_Work_Main_Group where f_mwsid in(select f_id from TS_TR_PS_Work_Main_Steps where f_mwdid in (select f_id from TS_TR_PS_Work_Main_Day where f_mwid in (select f_id from TS_TR_PS_Work_Main where f_del=0) and f_del=0) and f_del=0) and f_del=0 order by f_mwid,f_mwdid,f_mwsid,f_order asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _tl = new TR_PS_Work_Main_List();
                _tl.f_id = "G" + read["f_id"].ToString();
                _tl.f_order = read["f_order"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_mwid = read["f_mwid"].ToString();
                _tl.f_mwdid = read["f_mwdid"].ToString();
                _tl.f_mwsid = read["f_mwsid"].ToString();
                _tl.f_mwgid = read["f_id"].ToString();
                _tl.f_length = read["f_length"].ToString() + "分钟";
                _tl.f_stime = read["f_stime"].ToString();
                _tl.f_etime = read["f_etime"].ToString();
                _tl.f_parent = "S" + read["f_mwsid"].ToString();
                _tl.f_imgindex = 13;
                pList.Add(_tl);
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            tl_WorkFlow.DataSource = pList;
        }
    }
}